<template>
  <div class="menu">
    <div class="logo">
      <img class="img" src="~@/assets/images/logo.svg" alt="logo" />
      <span v-if="!collapse" class="title">Vue3-CMS</span>
    </div>

    <el-menu
      :default-active="defaultValue"
      background-color="#0c2135"
      active-text-color="#0a60bd"
      class="el-menu-vertical"
      text-color="#b7bdc3"
    >
      <template v-for="item in userMenu" :key="item.id">
        <el-sub-menu v-if="item.type === 1" :index="item.id + ''">
          <template #title>
            <el-icon><location /></el-icon>
            <span>{{ item.name }}</span>
          </template>
          <el-menu-item-group
            v-for="subItem in item.children"
            :key="subItem.id"
          >
            <el-menu-item
              :index="subItem.id + ''"
              @click="handleUrl(subItem)"
              >{{ subItem.name }}</el-menu-item
            >
          </el-menu-item-group>
        </el-sub-menu>

        <el-menu-item v-else-if="item.type === 2" :index="item.id + ''">
          <el-icon><document /></el-icon>
          <template #title>{{ item.name }}</template>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script>
import { computed } from "vue";
import localCache from "@/utils/cache";
import router from "@/router";
import { ref } from "vue";
import { useRoute } from "vue-router";
import { menuToBreadcrumb } from "@/utils/map-menu";
export default {
  setup() {
    const route = useRoute();
    const menus = localCache.getItem("menuInfo");
    const userMenu = computed(() => menus);
    const currentPath = route.path;
    const menu = menuToBreadcrumb(userMenu.value, currentPath);
    const defaultValue = ref(menu.id + "");

    const collapse = ref(false);

    const handleUrl = (item) => {
      router.push(item.url ?? "/not-found");
    };
    return {
      userMenu,
      handleUrl,
      defaultValue,
      collapse,
    };
  },
};
</script>

<style lang="less" scoped>
.menu {
  height: 100%;
  background-color: #001529;
  // overflow: auto;

  .logo {
    display: flex;
    height: 48px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }

  // 目录

  // hover 高亮
}
.el-menu {
  border-right: none;
  ::v-deep(.el-sub-menu__title) {
    background-color: #001529 !important;
    user-select: none;
  }
  .el-sub-menu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
    .el-menu-item:hover {
      color: #fff !important; // 菜单
      background-color: #001529 !important;
    }
    .el-menu-item.is-active {
      color: #fff !important;
      background-color: #0a60bd !important;
    }
  }
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
